package com.win2014.bet888;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.LinkedList;

import com.win2014.DB.ConnectionPool.PooledConnection;
import com.win2014.DB.DBConnect;

public class WinMain {
	
	public static boolean fetchData() {
		
		String sql = "select max(matchDay) from win_betGames";
		
		PooledConnection connection = DBConnect.getConnection();
		
		try {
			
			ResultSet result = connection.executeQuery(sql);
			
			connection.close();
			
			if (result.next()) {
				
				String day = result.getString(1);
				System.out.println("maxDay:" + day);
				
				Date date = new Date();
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				
				boolean processed = false;
				
				for (int i = 0; i < 999; i++) {
					date = getPriviouslyDay(date);
					
					if (formatter.format(date).equals(day)) {
						break;
					}
					
					processed = true;
					
					String dateString = formatter.format(date).replaceAll("-0", "-");
					System.out.println(dateString);
					WinWithMixedGames processor = new WinWithMixedGames();
					processor.process(dateString,WinWithMixedGames.PROCESS_DATASYNC);
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
				if (processed) {
					return true;
				} else {
					return false;
				}
				
			} 
			
			return false;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean fetchHistoryData(String dt) {
		
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		
		String sql = "select max(matchDay) from win_betGames";
		
		PooledConnection connection = DBConnect.getConnection();
		
		try {
			
			ResultSet result = connection.executeQuery(sql);
			
			connection.close();
			
			if (result.next()) {
				
				String day = result.getString(1);
				System.out.println("maxDay:" + day);
				
				Date date = formatter.parse(dt);
				
				boolean processed = false;
				
				for (int i = 0; i < 365; i++) {
					date = getPriviouslyDay(date);
					
					if (formatter.format(date).equals(day)) {
						break;
					}
					
					String dateString = formatter.format(date).replaceAll("-0", "-");
					System.out.println(dateString);
					WinWithMixedGames processor = new WinWithMixedGames();
					processor.process(dateString,WinWithMixedGames.PROCESS_DATASYNC);
					
					processor = null;
					try {
						Thread.sleep(10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					System.gc();
				}
				
				if (processed) {
					return true;
				} else {
					return false;
				}
				
			} 
			
			return false;
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	
	
	public static void main(String[] args) {
		
//		Date date = new Date();
//		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
//		
//		
//		for (int i = 0; i < 1; i++) {
////			date = getPriviouslyDay(date);
//			
//			String dateString = formatter.format(date).replaceAll("-0", "-");
//			System.out.println(dateString);
//			WinWithMixedGames processor = new WinWithMixedGames();
//			System.out.println(processor.process(dateString,WinWithMixedGames.PROCESS_FHLPR));
//			try {
//				Thread.sleep(10);
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
//		}
		
//		getAsianHerfs();
		
		fetchHistoryData("2013-03-12");
		
	}
	
	
	public static LinkedList<Hashtable<String, String>> getAsianHerfs () {
		
//		AsianOddsFetcher fetcher = new AsianOddsFetcher();
//		fetcher.processAsianOdds(671001, "/handicap/818268.html");
		
		String sql = "select matchSeriNO , asianherf from win_betgames";
			
		PooledConnection connection = DBConnect.getConnection();
		
		try {
			
			ResultSet result = connection.executeQuery(sql);
			
			connection.close();
			
			while (result.next()) {
				
				int matchSeriNO = result.getInt(1);
				String herf = result.getString(2);
				System.out.println(matchSeriNO + ":" + herf);
				
				AsianOddsFetcher fetcher = new AsianOddsFetcher();
				
//				fetcher.processAsianOdds(matchSeriNO, herf);
				
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return null;
	}

	public static Date getPriviouslyDay (Date date) {
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, -1);
		Date lastDay = calendar.getTime();
		return lastDay;
	}
	
}
